package com.tcm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tcm.entity.ExerciseHis;
import com.tcm.entity.ExerciseHisDetail;
import com.tcm.entity.dto.ExerciseHisDetailUpdateDto;
import com.tcm.entity.dto.ExerciseHisUpdateDto;
import com.tcm.mapper.ExerciseHisDetailMapper;
import com.tcm.service.ExerciseHisService;
import com.tcm.mapper.ExerciseHisMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author Guqier
* @description 针对表【ts_exercise_his(实训记录表)】的数据库操作Service实现
* @createDate 2023-09-13 20:07:29
*/
@Service
public class ExerciseHisServiceImpl extends ServiceImpl<ExerciseHisMapper, ExerciseHis>
    implements ExerciseHisService{

    @Autowired
    private ExerciseHisDetailMapper detailMapper;

    /**
     * 获取练习记录
     * @param exerciseId 练习ID
     * @param account    学生账号
     * @return 练习记录
     */
    @Override
    public ExerciseHisUpdateDto getExerciseHis(Long exerciseId, String account){
        ExerciseHis one = this.getOne(new LambdaQueryWrapper<ExerciseHis>().eq(ExerciseHis::getExerciseId, exerciseId).eq(ExerciseHis::getAccount, account));
        if (Objects.isNull(one)) {
            return null;
        }
        List<ExerciseHisDetail> details = detailMapper.selectList(new LambdaQueryWrapper<ExerciseHisDetail>().eq(ExerciseHisDetail::getExerciseHisId, one.getId()));
        ExerciseHisUpdateDto dto = new ExerciseHisUpdateDto();
        BeanUtils.copyProperties(one, dto);
        dto.setDetails(details.stream().map(detail -> {
            ExerciseHisUpdateDto.Detail d = new ExerciseHisUpdateDto.Detail();
            BeanUtils.copyProperties(detail, d);
            return d;
        }).collect(Collectors.toList()));
        return dto;
    }

    /**
     * 初始化练习记录DTO
     * @param exerciseId 练习ID
     * @param account 学生账号
     * @param testPaperId 试卷ID
     * @param itemCount 题目总数
     * @return DTO
     */
    @Override
    public ExerciseHisUpdateDto initExerciseUpdateDto(Long exerciseId, String account, Long testPaperId, Integer itemCount){
        ExerciseHisUpdateDto exerciseHisUpdateDto = new ExerciseHisUpdateDto();
        exerciseHisUpdateDto.setExerciseId(exerciseId);
        exerciseHisUpdateDto.setAccount(account);
        exerciseHisUpdateDto.setTestPaperId(testPaperId);
        exerciseHisUpdateDto.setCompletionStatus(0);
        exerciseHisUpdateDto.setScore(0);
        exerciseHisUpdateDto.setConsumeTime(0);
        exerciseHisUpdateDto.setCorrectCount(0);
        exerciseHisUpdateDto.setItemCount(itemCount);
        exerciseHisUpdateDto.setDetails(Collections.emptyList());
        return exerciseHisUpdateDto;
    }

    /**
     * 保存练习记录
     * @param dto 练习记录DTO
     * @param currUserId 当前登录用户ID
     * @return 保存结果
     */
    @Override
    @Transactional
    public boolean saveExerciseHis(ExerciseHisUpdateDto dto, Long currUserId){
        ExerciseHis exerciseHis = new ExerciseHis();
        BeanUtils.copyProperties(dto, exerciseHis);
        exerciseHis.setCreateUser(currUserId);
        exerciseHis.setUpdateUser(currUserId);
        return this.save(exerciseHis);
    }

    /**
     * 保存练习记录详细
     * @param dto 练习记录详细数据
     * @param currUserId 当前登录用户ID
     * @return 处理结果
     */
    @Override
    @Transactional
    public boolean saveExerciseHisDetail(ExerciseHisDetailUpdateDto dto, Long currUserId){
        ExerciseHisDetail exerciseHisDetail = new ExerciseHisDetail();
        BeanUtils.copyProperties(dto, exerciseHisDetail);
        exerciseHisDetail.setCreateUser(currUserId);
        exerciseHisDetail.setUpdateUser(currUserId);
        return detailMapper.insert(exerciseHisDetail) == 1;
    }

    /**
     * 保存练习详细
     * @param detail 练习详细
     * @return 保存结果
     */
    @Override
    @Transactional
    public boolean saveExerciseHisDetail(ExerciseHisDetail detail){
        return detailMapper.insert(detail) == 1;
    }

    /**
     * 标记完成练习
     * @param exerciseId 练习ID
     * @param account 账号
     * @return 处理结果
     */
    @Override
    @Transactional
    public boolean completeExerciseHis(Long exerciseId, String account){
        ExerciseHis exerciseHis = this.getOne(new LambdaQueryWrapper<ExerciseHis>().eq(ExerciseHis::getExerciseId, exerciseId).eq(ExerciseHis::getAccount, account));
        if (Objects.isNull(exerciseHis)) {
            return false;
        }
        exerciseHis.setCompletionStatus(1);
        return this.updateById(exerciseHis);
    }

}




